home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / misc / array.c < prev    next >
C/C++ Source or Header  |  1996-01-15  |  4KB  |  229 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "misc.h"
  4. #include "../xconf/xconf.h"
  5.  
  6. PUBLIC ARRAY_OBJ::ARRAY_OBJ()
  7. {
  8.     modified = 0;
  9. }
  10.  
  11. PUBLIC VIRTUAL ARRAY_OBJ::~ARRAY_OBJ()
  12. {
  13. }
  14. /*
  15.     Reset the modify flags of the element.
  16. */
  17. PUBLIC void ARRAY_OBJ::rstmodified()
  18. {
  19.     modified = 0;
  20. }
  21. /*
  22.     Reset the modify flags of the element.
  23. */
  24. PUBLIC void ARRAY_OBJ::setmodified()
  25. {
  26.     modified = 1;
  27. }
  28. /*
  29.     Return true if the element was modified.
  30. */
  31. PUBLIC VIRTUAL int ARRAY_OBJ::was_modified()
  32. {
  33.     return modified;
  34. }
  35.  
  36. /*
  37.     Manage the content of a hosts file (generally /etc/hosts
  38. */
  39. PUBLIC ARRAY::ARRAY()
  40. {
  41.     tb = NULL;
  42.     nb = 0;
  43.     maxtb = 0;
  44.     modified = 0;
  45.     increm = 100;
  46.     is_owner = 1;
  47. }
  48. /*
  49.     Instruct this array it is not allowed to delete objects.
  50. */
  51. PUBLIC void ARRAY::neverdelete()
  52. {
  53.     is_owner = 0;
  54. }
  55.  
  56. /*
  57.     Record the new growth rate of the internal array. Each time
  58.     the array overflow, a larger is realloced. It will be _increm
  59.     item larger.
  60. */
  61. PUBLIC void ARRAY::setgrowth(int _increm)
  62. {
  63.     increm = _increm;
  64. }
  65. PUBLIC VIRTUAL ARRAY::~ARRAY()
  66. {
  67.     if (is_owner) for (int i=0; i<nb; i++) delete tb[i];
  68.     free (tb);
  69. }
  70.  
  71. /*
  72.     Make sure there is enough space in the ARRAY_OBJ * table
  73. */
  74. PROTECTED void ARRAY::grow()
  75. {
  76.     if (nb == maxtb){
  77.         maxtb += increm;
  78.         tb = (ARRAY_OBJ**)realloc(tb,maxtb*sizeof(ARRAY_OBJ*));
  79.         if (tb == NULL){
  80.             xconf_error ("Out of memory\n");
  81.             exit (-1);
  82.         }
  83.     }
  84. }
  85.  
  86. /*
  87.     Remove one entry from the list.
  88.     The entry is not freed. The caller must do it.
  89.  
  90.     Return -1 if the entry was not found.
  91. */
  92. PUBLIC int ARRAY::remove(ARRAY_OBJ *obj)
  93. {
  94.     int j=0;
  95.     int ret = -1;
  96.     for (int i=0; i<nb; i++){
  97.         ARRAY_OBJ *tbi = tb[i];
  98.         if (tbi != obj){
  99.             if (j != i) tb[j] = tbi;
  100.             j++;
  101.         }else{
  102.             modified = 1;
  103.             ret = 0;
  104.         }
  105.     }
  106.     nb = j;
  107.     return ret;
  108. }
  109. /*
  110.     Remove all entry from the list without destroying the objects.
  111. */
  112. PUBLIC void ARRAY::remove_all()
  113. {
  114.     nb = 0;
  115. }
  116. /*
  117.     Remove one entry from the list and delete the object.
  118.     If the object is not part of the list, it is not deleted.
  119.  
  120.     Return -1 if the entry was not found.
  121. */
  122. PUBLIC void ARRAY::remove_del(ARRAY_OBJ *obj)
  123. {
  124.     if (remove(obj)!=-1 && is_owner) delete obj;
  125. }
  126.  
  127. PUBLIC void ARRAY::delall()
  128. {
  129.     if (is_owner) for (int i=0; i<nb; i++) delete tb[i];
  130.     nb = 0;
  131.     modified = 1;
  132. }
  133.  
  134. /*
  135.     Return the number of entry in a table.
  136. */
  137. PUBLIC int ARRAY::getnb() const
  138. {
  139.     return nb;
  140. }
  141.  
  142. /*
  143.     Add one item to the in memory hosts table
  144. */
  145. PUBLIC void ARRAY::add (ARRAY_OBJ *pt)
  146. {
  147.     if (pt != NULL){
  148.         grow();
  149.         tb[nb++] = pt;
  150.         modified = 1;
  151.     }
  152. }
  153.  
  154. /*
  155.     Insert one item to the table
  156. */
  157. PUBLIC void ARRAY::insert (int pos, ARRAY_OBJ *pt)
  158. {
  159.     if (pt != NULL){
  160.         if (pos >= nb){
  161.             add (pt);
  162.         }else{
  163.             grow();
  164.             memmove (tb+pos+1,tb+pos,(nb-pos)*sizeof(tb[0]));
  165.             tb[pos] = pt;
  166.             nb++;
  167.         }
  168.         modified = 1;
  169.     }
  170. }
  171. /*
  172.     This function should be duplicate in each sub-class to allow
  173.     proper casting.
  174. */
  175. PROTECTED ARRAY_OBJ *ARRAY::getitem (int no) const
  176. {
  177.     ARRAY_OBJ *ret = NULL;
  178.     if (no >= 0 && no < nb) ret = tb[no];
  179.     return ret;
  180. }
  181.     
  182.  
  183. /*
  184.     Reset the modify flags of all elements of the array.
  185. */
  186. PUBLIC void ARRAY::rstmodified()
  187. {
  188.     int nbo = getnb();
  189.     for (int i=0; i<nbo; i++) tb[i]->rstmodified();
  190.     modified = 0;
  191. }
  192. /*
  193.     Return != if the element was modified.
  194. */
  195. PUBLIC VIRTUAL int ARRAY::was_modified()
  196. {
  197.     int ret  = modified;
  198.     if (!ret){
  199.         int nbo = getnb();
  200.         for (int i=0; i<nbo; i++){
  201.             int modif = tb[i]->was_modified();
  202.             if (modif){
  203.                 ret = true;
  204.                 break;
  205.             }
  206.         }
  207.     }
  208.     return ret;
  209. }
  210.  
  211. static int (*user_cmp) (const ARRAY_OBJ *, const ARRAY_OBJ *);
  212. static int local_cmp (const void *pt1, const void *pt2)
  213. {
  214.     return (*user_cmp)(*(ARRAY_OBJ**)pt1,*(ARRAY_OBJ**)pt2);
  215. }
  216.  
  217. /*
  218.     Sort the ARRAY with a user function working like the one
  219.     for qsort.
  220.  
  221.     Use qsort() internally.
  222. */
  223. PUBLIC void ARRAY::sort (int (*cmp) (const ARRAY_OBJ *, const ARRAY_OBJ *))
  224. {
  225.     user_cmp = cmp;
  226.     qsort (tb,nb,sizeof(ARRAY_OBJ*),local_cmp);
  227. }
  228.  
  229.